import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)

// 解决vue-router的bug
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
    routes: [
        {
            path: '/login',
            component: () => import('../components/pages/login.vue')
        },
        {
            path: '/',
            component: () => import('../components/pages/layout.vue'),
            redirect: '/home',
            // 添加一个元数据，然后使用导航守卫
            meta: {requiresAuth: true},
            children: [
                {
                    path: '/home',
                    component: () => import('../components/pages/home')
                },{
                    path: '/user',
                    component: () => import('../components/pages/user/index')
                },{
                    path: '/user/add',
                    component: () => import('../components/pages/user/edit')
                },{
                    path: '/user/:id',
                    component: () => import('../components/pages/user/edit')
                },{
                    path: '/menu',
                    component: () => import('../components/pages/menu/index')
                },{
                    path: '/menu/add',
                    component: () => import('../components/pages/menu/edit')
                },{
                    path: '/menu/:id',
                    component: () => import('../components/pages/menu/edit')
                },{
                    path: '/role',
                    component: () => import('../components/pages/role/index')
                },{
                    path: '/role/add',
                    component: () => import('../components/pages/role/edit')
                },{
                    path: '/role/:id',
                    component: () => import('../components/pages/role/edit')
                },{
                    path: '/cate',
                    component: () => import('../components/pages/cate/index')
                },{
                    path: '/cate/add',
                    component: () => import('../components/pages/cate/edit')
                },{
                    path: '/cate/:id',
                    component: () => import('../components/pages/cate/edit')
                },{
                    path: '/specs',
                    component: () => import('../components/pages/specs/index')
                },{
                    path: '/specs/add',
                    component: () => import('../components/pages/specs/edit')
                },{
                    path: '/specs/:id',
                    component: () => import('../components/pages/specs/edit')
                },{
                    path: '/goods',
                    component: () => import('../components/pages/goods/index')
                },{
                    path: '/goods/add',
                    component: () => import('../components/pages/goods/edit')
                },{
                    path: '/goods/:id',
                    component: () => import('../components/pages/goods/edit')
                }
            ]
        }
    ]
})

// 全局前置守卫，验证是否登录
router.beforeEach((to, from, next)=>{
    console.log(to);
    // 判断目标路由是否需要登录才能访问
    if(to.matched.some(recored=>recored.meta.requiresAuth)){
        // 进一步判断用户是否已经登录
        if(store.state.userInfo.token){
            next()
        }else{
            next({
                path: '/login',
                query: {redirect: to.fullPath}
            })
        }
    }else{
        next()
    }
})


export default router;